//
//  DSBaseViewController.m
//  DSJY
//
//  Created by 冯华强 on 16/9/24.
//  Copyright © 2016年 dsjy. All rights reserved.
//  父控制器

#import "DSBaseViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "DSLoginController.h"
#import "MTNavigationController.h"
#import "UIStoryboard+Extension.h"
#import "DSAccountViewModel.h"
#import "UIWindow+Extension.h"
#import "MBProgressHUD+MJ.h"

@implementation DSBaseViewController

#pragma mark - 懒加载
- (RACSubject *)networkWiFiSignal
{
    if (_networkWiFiSignal == nil){
        _networkWiFiSignal = [RACSubject subject];
    }
    return _networkWiFiSignal;
}

- (RACSubject *)networkWWANSignal
{
    if (_networkWWANSignal == nil){
        _networkWWANSignal = [RACSubject subject];
    }
    return _networkWWANSignal;
}

- (RACSubject *)networkWiFiWWANSignal
{
    if (_networkWiFiWWANSignal == nil){
        _networkWiFiWWANSignal = [RACSubject subject];
    }
    return _networkWiFiWWANSignal;
}

- (RACSubject *)networkUnknownNotReachableSignal
{
    if (_networkUnknownNotReachableSignal == nil){
        _networkUnknownNotReachableSignal = [RACSubject subject];
    }
    return _networkUnknownNotReachableSignal;
}

#pragma mark - 系统回调
- (void)viewDidLoad
{
    [super viewDidLoad];

    //键盘显示
    [[[MTNotificationCenter rac_addObserverForName:YQResponseCodeTokenInvalidNote object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *noti) {
        
        [DSAccountViewModel sharedAccount].account = nil;
        [[DSAccountViewModel sharedAccount] write];
        [UIWindow switchRootVCToAppDelegateWin];
        [MBProgressHUD showText:@"用户登录凭证过期，请重新登录!"];
        
//        //提示
//        NSString *msg = [NSString stringWithFormat:@"%@，用户登录凭证已失效，是否重新登录?", noti.object];
//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//        UIAlertAction *submitAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            //跳转登录控制器
//            DSLoginController *loginVC = (DSLoginController *)[UIStoryboard initialVCWithName:@"DSLoginController"];
//            loginVC.title = @"登录";
//            loginVC.showBackItem = YES;
//            MTNavigationController *navVC = [[MTNavigationController alloc] initWithRootViewController:loginVC];
//            [self presentViewController:navVC animated:YES completion:nil];
//        }];
//        [alertController addAction:cancelAction];
//        [alertController addAction:submitAction];
//        
//        [self presentViewController:alertController animated:YES completion:nil];
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //结束编辑
    [self.view endEditing:YES];
}

- (void)dealloc
{
    [SVProgressHUD dismiss];
}

#pragma mark - setter
- (void)setShowBackItem:(BOOL)showBackItem
{
    _showBackItem = showBackItem;
    
    if (self.isShowBackItem) {
        [self setupBackBarItem];
    }
}

#pragma mark - 设置UI
- (void)setupBackBarItem
{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemMapWithTarget:self action:@selector(back) image:@"icon_back" selectImage:@"icon_back_highlighted"];
}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 外部方法
- (void)checkNetWorkWithRefreshType:(YQRefreshType)refreshType
{
    YQAFNetworkReachabilityTool *networkTool = [YQAFNetworkReachabilityTool sharedTool];
    AFNetworkReachabilityStatus status = networkTool.status;
    NSArray *send = @[@(refreshType), @(status)];
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWiFi:     //WiFi
            [self.networkWiFiSignal sendNext:send];
            [self.networkWiFiWWANSignal sendNext:send];  //WiFi2G/3G/4G网络
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:     //2G/3G/4G网络
            [self.networkWWANSignal sendNext:send];
            [self.networkWiFiWWANSignal sendNext:send];  //WiFi2G/3G/4G网络
            break;
        case AFNetworkReachabilityStatusUnknown:              //无网络
        case AFNetworkReachabilityStatusNotReachable:
            [self.networkUnknownNotReachableSignal sendNext:send];
            break;
        default:
            break;
    }
}

- (void)reloadDataWithTableView:(UITableView *)tableView currentPage:(NSInteger)currentPage totalPage:(NSInteger)totalPage
{    //刷新表格
    [tableView reloadData];
    
    //结束上下拉状态、更多数据状态
    [self endRefreshingWithScrollView:tableView currentPage:currentPage totalPage:totalPage];
}

- (void)reloadDataWithCollectionView:(UICollectionView *)collectionView currentPage:(NSInteger)currentPage totalPage:(NSInteger)totalPage
{
    //刷新表格
    [collectionView reloadData];
    
    //结束上下拉状态、更多数据状态
    [self endRefreshingWithScrollView:collectionView currentPage:currentPage totalPage:totalPage];
}

- (void)endRefreshingWithScrollView:(UIScrollView *)scrollView currentPage:(NSInteger)currentPage totalPage:(NSInteger)totalPage
{
    //结束上下拉刷新
    [scrollView.mj_header endRefreshing];
    [scrollView.mj_footer endRefreshing];
    
    //判断时候已经到尾页
    if (currentPage >= totalPage) {
        // 拿到当前的上拉刷新控件，变为没有更多数据的状态
        [scrollView.mj_footer endRefreshingWithNoMoreData];
    }
}

- (void)showEmptyHUDWithView:(nullable UIScrollView *)scrollView
{
    if (scrollView != nil) {
        if ([scrollView isKindOfClass:[UICollectionView class]]) { //collectionView
            UICollectionView *collectionView = (UICollectionView *)scrollView;
            [collectionView reloadData];
        }else if([scrollView isKindOfClass:[UITableView class]]){
            UITableView *tableView = (UITableView *)scrollView;
            [tableView reloadData];
        }
        //结束上下拉刷新
        [scrollView.mj_header endRefreshing];
        [scrollView.mj_footer endRefreshing];
    }
    //提示
    [SVProgressHUD showErrorWithStatus:@"网络不给力，请稍后再试"];
}

@end
